<?php

namespace Osp\OrderBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Osp\OrderBundle\Entity\Orders;
use JMS\SecurityExtraBundle\Annotation\Secure; 

class OrderController extends Controller
{
	/**
   	* @Secure(roles="ROLE_ADMIN")
   	*/
    public function orderManagementAction()
    {

    	$error = null;
    	//Récupération des commandes dans la base
	   	$repository = $this->getDoctrine()->getManager()->getRepository('OspOrderBundle:Orders');
		$liste_orders = $repository->findAll();

		if (!$liste_orders) 
		{ 
			$error = "Aucune commande dans la Base de données";
		}


		//Création du formulaire
		$order = new Orders();

		// On crée le FormBuilder grâce à la méthode du contrôleur
		$formBuilder = $this->createFormBuilder($order);

		$formBuilder
		->add('utilisateur', 'entity', array('class' => 'OspUserBundle:User','property' => 'nom'))
		->add('listeProduits', 'entity', array('class' => 'OspProductBundle:Product','property' => 'libelle', 'multiple' => true))
		->add('status', 'text');

		$form = $formBuilder->getForm();

		//On récupère la requête
		$request = $this->get('request');


		//Si la requête est de type POST
		if($request->getMethod() == 'POST')
		{
			//order contient maintenant les valeurs du formulaire
			$form->bind($request);
			
			if($form->isValid())
			{
				$entityManager = $this->getDoctrine()->getManager();
				$entityManager->persist($order);
				$entityManager->flush();

				return $this->redirect($this->generateUrl('osp_order_homepage'));
			}

				else
				{
					return $this->render('OspOrderBundle:Order:errors.html.twig', array('error' => $form->getErrorsAsString()));
				}

		}

		else
		{
        	return $this->render('OspOrderBundle:Order:orderManagement.html.twig', array('orders' => $liste_orders, 'form' => $form->createView(), 'error' => $error));
    	}
    }

    /**
   	* @Secure(roles="ROLE_ADMIN")
   	*/
    public function deleteOrderAction($id) 
	{ 
		$entityManager = $this->getDoctrine()->getManager(); 
		$order = $entityManager->getRepository('OspOrderBundle:Orders')->find($id); 

		if (!$order) 
			{ 
				throw $this->createNotFoundException('No order found for id '.$id); 
			} 

		$entityManager->remove($order); 
		$entityManager->flush(); 

		return $this->redirect($this->generateUrl('osp_order_homepage')); 
	}

	/**
   	* @Secure(roles="ROLE_ADMIN")
   	*/
	public function updateOrderAction($id)
	{ 
		$entityManager = $this->getDoctrine()->getManager(); 
		$order = $entityManager->getRepository('OspOrderBundle:Orders')->find($id); 

		if (!$order) 
			{ 
				throw $this->createNotFoundException('No order found for id '.$id);
			}


		// On crée le FormBuilder grâce à la méthode du contrôleur
		$formBuilder = $this->createFormBuilder($order);

		$formBuilder
		->add('utilisateur', 'entity', array('class' => 'OspUserBundle:User','property' => 'nom'))
		->add('listeProduits', 'entity', array('class' => 'OspProductBundle:Product','property' => 'libelle', 'multiple' => true))
		->add('status', 'text');

		$form = $formBuilder->getForm();

		//On récupère la requête
		$request = $this->get('request');


		//Si la requête est de type POST
		if($request->getMethod() == 'POST')
		{
			//order contient maintenant les valeurs du formulaire
			$form->bind($request);
			
			if($form->isValid())
			{
				$entityManager = $this->getDoctrine()->getManager();
				$entityManager->flush();

				return $this->redirect($this->generateUrl('osp_order_homepage'));
			}

				else
				{
					return $this->render('OspOrderBundle:Order:errors.html.twig', array('error' => $form->getErrorsAsString()));
				}

		}	 

		return $this->render('OspOrderBundle:Order:orderUpdate.html.twig', array('order' => $order, 'form' => $form->createView())); 
	}

}
